package com.dunqi.gpm.cangxi.home.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.FragmentCallBack;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.fragment.ProjectListFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;

public class ProjectListActivity extends BaseAct implements FragmentCallBack {
    @Override
    public void callBcakUri(Uri uri) {
        if ("refresh".equals(uri.getScheme())) {
            titleBar.setRightText(uri.getHost());
        }
    }

    private static final String DATA = "list_photo";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;

    private HashMap<String, String> params;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        if (getIntent() != null) {
            params = (HashMap<String, String>) getIntent().getSerializableExtra(DATA);
        }
        replace();
        titleBar.setOnClickListenerOfRight(v -> {
            Intent intent = new Intent(getActivity(), ProjectInquiryActivity.class);
            PopupMenu pop = new PopupMenu(this, v);//v是加号控件
            pop.getMenuInflater().inflate(R.menu.project_serch_menu, pop.getMenu());
            pop.show();
            pop.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()) {
                    case R.id.action_all:
                        EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("all"));
                        titleBar.setRightText("全部项目");
                        break;
                    case R.id.action_shida:
                        EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("十件大事"));
                        titleBar.setRightText("十件大事");
                        break;
                    case R.id.action_zhongdian:
                        EventBus.getDefault().post(new ProjectListFragment.EvnetbusMSG("重点项目"));
                        titleBar.setRightText("重点项目");
                        break;
                }
                return false;
            });
//            startActivityForResult(intent, 100);
        });
    }

    private void replace() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, ProjectListFragment.newInstance(params, 0))
                .commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 400) {
            params = (HashMap<String, String>) data.getSerializableExtra("list_photo");
            replace();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_my_follow1;
    }

    public static void start(Context context, HashMap<String, String> params) {
        Intent intent = new Intent(context, ProjectListActivity.class);
        intent.putExtra(DATA, params);
        context.startActivity(intent);
    }
}